﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PMS.SANY.Util;

namespace PMS.SANY.Entity
{
    /// <summary>
    /// 系统配置
    /// </summary>
    public partial class CF_Config : BusEntity
    {
        /// <summary>
        /// 仓库Id
        /// </summary>
        public string CompanyId { get; set; }
        /// <summary>
        /// 参数编码
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 参数名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 参数类型
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 可选值配置
        /// </summary>
        public string ValConfig { get; set; }
        /// <summary>
        /// 配置值
        /// </summary>
        public string Val { get; set; }
        /// <summary>
        /// 是否系统必需
        /// </summary>
        public bool IsSystem { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
    }

    public partial class CF_Config : BusEntity
    {
        /// <summary>
        /// 仓库
        /// </summary>
        //public Bas_Whse Whse { get; set; }
    }

    /// <summary>
    /// 实体配置
    /// OnModelCreating
    /// </summary>
    public class CF_ConfigTypeConfig : BusEntityTypeConfig<CF_Config>, IEntityTypeConfiguration<CF_Config>
    {
        public override void Configure(EntityTypeBuilder<CF_Config> builder)
        {
            base.Configure(builder);

            #region 主外键关系
            //builder.HasOne(o => o.Whse).WithMany().HasForeignKey(p => p.WhseId).OnDelete(DeleteBehavior.ClientCascade);
            #endregion

            #region 字段属性:最大长度,是否必需,默认值
            builder.Property(p => p.Code).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.Name).HasMaxLength(EntityDefinition.DefMiddleColLen).IsRequired();
            builder.Property(p => p.Type).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.ValConfig).HasMaxLength(EntityDefinition.DefLargeColLen);
            builder.Property(p => p.Val).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.IsSystem).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired().HasDefaultValue(false);
            builder.Property(p => p.Remark).HasMaxLength(EntityDefinition.DefLargeColLen);
            #endregion

            #region 备注
            builder.HasComment("系统配置");
            builder.Property(p => p.Code).HasComment("参数编码");
            builder.Property(p => p.Name).HasComment("参数名称");
            builder.Property(p => p.Type).HasComment("参数类型");
            builder.Property(p => p.ValConfig).HasComment("可选值配置");
            builder.Property(p => p.Val).HasComment("配置值");
            builder.Property(p => p.IsSystem).HasComment("是否系统必需");
            builder.Property(p => p.Remark).HasComment("备注");
            #endregion

            #region 种子数据
            builder.HasData(new CF_Config() { Id = $"{EntityDefinition.DefSysCode}_AppName", CompanyId = EntityDefinition.DefSysCode, Code = "AppName", Name = "系统名称", Type = "System", ValConfig = "", Val = "搅拌筒生产线生产管理系统", IsSystem = true, Remark = "", CreateUserId = "1", ModifyUserId = "1" });
            builder.HasData(new CF_Config() { Id = $"{EntityDefinition.DefSysCode}_Sys_User_Code_AutoGenerate", CompanyId = EntityDefinition.DefSysCode, Code = "Sys_User_Code_AutoGenerate", Name = "用户编码自动生成", Type = "User", ValConfig = "", Val = "1", IsSystem = false, Remark = "", CreateUserId = "1", ModifyUserId = "1" });
            #endregion
        }
    }
}
